﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class SpecialNewsCategories : YuanlinUserControl,
        IReplyConverter<AreaReply, GetListOperation>
    {
        protected Repeater provinceList;
        protected Repeater cityList;
        protected Repeater townList;
        protected Repeater smallTownList;
        protected HtmlControl cityDiv;
        protected HtmlControl townDiv;
        protected HtmlControl smallTownDiv;
        protected ValuedTextBox searchTextBox;
        protected IButton SearchButton;
        private EntityList areas;
        public int provinceId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.ProvinceId);
        public int cityId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CityId);
        public int townId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.TownId);
        private int smallTownId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.SmallTownId);
        private int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);

        public override void GetRequests(List<BaseRequestBinder> requestBinders)
        {
            if (!Page.IsPostBack)
            {
                //找出所有的省
                AreaListRequest request = new AreaListRequest();
                request.UpAreaId = 0;
                request.PageIndex = 0;
                request.PageSize = Int32.MaxValue;
                requestBinders.Add(RequestHelper.BuildGetAreaListRequest(this, request));
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            if (this.SearchButton != null)
            {
                this.SearchButton.Click += new EventHandler(this.SearchButton_Click);
            }
        }

        private void Bind()
        {
            provinceList.DataSource = areas;
            provinceList.DataBind();
            if (areas.Count == 0)
            {
                cityDiv.Visible = false;
                townDiv.Visible = false;
                smallTownDiv.Visible = false;
                return;
            }

            AreaListRequest request = new AreaListRequest();
            request.UpAreaId = provinceId > 0 ? provinceId : Int32.MaxValue;
            request.PageIndex = 0;
            request.PageSize = 100;
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            cityList.DataSource = areas;
            cityList.DataBind();
            if (areas.Count == 0)
            {
                cityDiv.Visible = false;
                townDiv.Visible = false;
                smallTownDiv.Visible = false;
                return;
            }

            request = new AreaListRequest();
            request.UpAreaId = cityId > 0 ? cityId : Int32.MaxValue;
            request.PageIndex = 0;
            request.PageSize = 100;
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            townList.DataSource = areas;
            townList.DataBind();
            if (areas.Count == 0)
            {
                townDiv.Visible = false;
                smallTownDiv.Visible = false;
                return;
            }

            request = new AreaListRequest();
            request.UpAreaId = townId > 0 ? townId : Int32.MaxValue;
            request.PageIndex = 0;
            request.PageSize = 100;
            areas = BusinessManager.GetAreaList(RequestHelper.BuildGetAreaListRequest(request));
            smallTownList.DataSource = areas;
            smallTownList.DataBind();
            if (areas.Count == 0)
            {
                smallTownDiv.Visible = false;
            }
        }

        private void SearchButton_Click(object sender, System.EventArgs e)
        {
            string path = "";
            if (categoryType == (int)CategoryType.ZhiXiangNews)
            {
                path = SiteUrls.Instance().GetHuamuZhiXiangNewsListUrl(provinceId, cityId, townId, smallTownId);
            }
            else if (categoryType == (int)CategoryType.ShiChangNews)
            {
                path = SiteUrls.Instance().GetHuamuShichangNewsListUrl(provinceId, cityId, townId, smallTownId);
            }
            else if (categoryType == (int)CategoryType.HuiZhanNews)
            {
                path = SiteUrls.Instance().GetHuamuHuizhanNewsListUrl(provinceId, cityId, townId, smallTownId);
            }
            if (!string.IsNullOrEmpty(this.searchTextBox.Value))
            {
                if (path.IndexOf("?") > 0)
                {
                    path += string.Format("&QueryText={0}", System.Web.HttpUtility.UrlEncode(this.searchTextBox.Value));
                }
                else
                {
                    path += string.Format("?QueryText={0}", System.Web.HttpUtility.UrlEncode(this.searchTextBox.Value));
                }
            }

            Page.Response.Redirect(path);

        }

        #region IReplyConverter<AreaReply,GetListOperation> Members

        public void ConvertReply(AreaReply reply, GetListOperation operation)
        {
            if (reply != null)
            {
                areas = reply.EntityList;

                if (!Page.IsPostBack)
                {
                    Bind();
                }
            }
        }

        #endregion
    }
}
